package me.ezitku.base.extensions.language

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import java.util.*

/**
 * Created by yurqi on 2017/5/19.
 *ئەپنىڭ تىلى
 */

fun Context.languageContext(): Context {
    var ctx = this
    val locale = Locale(currentLanguagePref)
    val res = this.resources
    val config = Configuration(res.configuration)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale)
        ctx = this.createConfigurationContext(config)
    } else {
        config.locale = locale
        res.updateConfiguration(config, res.displayMetrics)
    }
    return ctx
}

@Deprecated("use Context.languageContext(Context)")
fun Context.initLanguage() {
    val dm = resources.displayMetrics
    val config = resources.configuration
    config.locale = Locale(currentLanguagePref)
    //var loca = Locale(currentLanguagePref)
    resources.updateConfiguration(config, dm)
}
